<?php
/*
Plugin Name: Site Core Features
Description: 日常黄历、梅花易数算卦、图文资讯浏览的核心功能与短码。
Version: 0.1.0
Author: Trae AI
*/

if (!defined('ABSPATH')) {
    exit;
}

// 常量
define('SCF_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('SCF_PLUGIN_URL', plugin_dir_url(__FILE__));

// 资源加载（简单样式）
function scf_enqueue_assets() {
    wp_register_style('scf-styles', SCF_PLUGIN_URL . 'assets/styles.css', [], '0.1.0');
    // 恢复脚本注册并改为头部加载（避免主题未调用 wp_footer）
    wp_register_script('scf-lunar', SCF_PLUGIN_URL . 'assets/lunar.js', [], '1.7.5', false);
    wp_register_script('scf-almanac', SCF_PLUGIN_URL . 'assets/almanac.js', ['scf-lunar'], '1.0.0', false);

    wp_enqueue_style('scf-styles');
    wp_enqueue_script('scf-lunar');
    wp_enqueue_script('scf-almanac');
}
add_action('wp_enqueue_scripts', 'scf_enqueue_assets');

// 管理页：配置资讯 RSS 源
function scf_register_settings() {
    register_setting('scf_settings_group', 'scf_news_feeds');
}
add_action('admin_init', 'scf_register_settings');

function scf_settings_page_render() {
    $feeds = get_option('scf_news_feeds', "https://36kr.com/feed\nhttps://feed.cnblogs.com/blog/sitehome/rss");
    ?>
    <div class="wrap">
        <h1>核心功能设置</h1>
        <form method="post" action="options.php">
            <?php settings_fields('scf_settings_group'); ?>
            <?php do_settings_sections('scf_settings_group'); ?>
            <table class="form-table" role="presentation">
                <tr>
                    <th scope="row"><label for="scf_news_feeds">资讯 RSS 源（一行一个）</label></th>
                    <td>
                        <textarea id="scf_news_feeds" name="scf_news_feeds" rows="8" cols="60"><?php echo esc_textarea($feeds); ?></textarea>
                        <p class="description">用于图文资讯浏览的 RSS 源列表。</p>
                    </td>
                </tr>
            </table>
            <?php submit_button(); ?>
        </form>
        <p>短码：<code>[daily_almanac]</code>、<code>[meihua_divination]</code>、<code>[news_feed]</code>、<code>[user_register]</code>、<code>[user_login]</code></p>
    </div>
    <?php
}

function scf_add_settings_page() {
    add_options_page('核心功能设置', '核心功能设置', 'manage_options', 'scf-settings', 'scf_settings_page_render');
}
add_action('admin_menu', 'scf_add_settings_page');

// 引入功能模块
require_once SCF_PLUGIN_DIR . 'includes/almanac.php';
require_once SCF_PLUGIN_DIR . 'includes/meihua.php';
require_once SCF_PLUGIN_DIR . 'includes/news.php';
require_once SCF_PLUGIN_DIR . 'includes/user.php';

// 注册短码
add_shortcode('daily_almanac', 'scf_daily_almanac_shortcode');
add_shortcode('meihua_divination', 'scf_meihua_divination_shortcode');
add_shortcode('news_feed', 'scf_news_feed_shortcode');
add_shortcode('user_register', 'scf_user_register_shortcode');
add_shortcode('user_login', 'scf_user_login_shortcode');